package com.example.controller;

import com.example.common.Result;
import com.example.entity.Department;
import com.example.entity.DepartmentOrderStatistics;
import com.example.entity.OrderDailyStatistics;
import com.example.entity.WorkOrder;
import com.example.service.WorkOrderService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/order")
public class WorkOrderController {

    @Resource
    private WorkOrderService workOrderService;

    /**
     * 分页查询
     */
    @GetMapping("/selectPage")
    public Result selectPage(WorkOrder workOrder,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<WorkOrder> page = workOrderService.selectPage(workOrder, pageNum, pageSize);
        return Result.success(page);
    }

    /**
     * 新增
     */
    @PostMapping("/add")
    public Result add(@RequestBody WorkOrder workOrder) {
        workOrder.setCreateTime(LocalDateTime.now());
        workOrderService.add(workOrder);
        return Result.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        workOrderService.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        workOrderService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result updateById(@RequestBody WorkOrder workOrder) {
        workOrderService.updateById(workOrder);
        return Result.success();
    }

    /**
     * 分派
     */
    @PostMapping("/fenpai")
    public Result fenpai(@RequestBody Department department) {
        workOrderService.fenpai(department);
        return Result.success();
    }

    /**
     * 查询7月每天的工单总量、超期率
     */
    @GetMapping("/statistics/daily")
    public Result getDailyOrderStatistics() {
        List<OrderDailyStatistics> statistics = workOrderService.getDailyOrderStatisticsForMonth(2024, 7);
        return Result.success(statistics);
    }

    /**
     * 查询7月每个部门的工单总量、超期率
     */
    @GetMapping("/statistics/department")
    public Result getDepartmentOrderStatistics() {
        List<DepartmentOrderStatistics> statistics = workOrderService.getDepartmentOrderStatisticsForMonth(2024, 7);
        return Result.success(statistics);
    }
}
